package com.larno.larnosqliteexample.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import com.larno.larnosqliteexample.R;
import com.larno.larnosqliteexample.database.bean.Todo;
import com.larno.larnosqliteexample.database.dao.TodoDao;
import com.larno.larnosqliteexample.database.provider.TodoContentProvider;

/**
 * Created by sks on 2016/7/4.
 */
public class DetailActivity extends AppCompatActivity {

    private EditText editSummary;
    private EditText editDescription;
    private Spinner spinnerCategory;

    private Uri todoUri;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        Bundle extras = getIntent().getExtras();
        if (todoUri != null){
            todoUri = extras.getParcelable(TodoContentProvider.CONTENT_ITEM_TYPE);
        }

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveTodo(view);
            }
        });
        spinnerCategory = (Spinner) findViewById(R.id.spinnerCategory);
        editSummary = (EditText) findViewById(R.id.editSummary);
        editDescription = (EditText) findViewById(R.id.editDescription);
    }

    /*保存编辑的日程提醒*/
    private void saveTodo(View view) {
        String category = (String) spinnerCategory.getSelectedItem();
        String summary = editSummary.getText().toString();
        String description = editDescription.getText().toString();
        Todo todo = new Todo(category, summary, description);

        Snackbar snackbar;
        if (todoUri == null) {
            Uri uri = TodoDao.insert(this, todo);
            snackbar = Snackbar.make(view, uri.toString(), Snackbar.LENGTH_LONG);

        } else {
            int rowsUpdated = TodoDao.update(this, todoUri, todo);
            snackbar = Snackbar.make(view, String.valueOf(rowsUpdated), Snackbar.LENGTH_LONG);
        }
        snackbar.setCallback(new Snackbar.Callback() {
            @Override
            public void onDismissed(Snackbar snackbar, int event) {
                super.onDismissed(snackbar, event);
            }

            @Override
            public void onShown(Snackbar snackbar) {
                super.onShown(snackbar);
            }
        });
        snackbar.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
